GridView class

您所在的位置:网站首页 flutter gridview GridView class

GridView class

#GridView class| 来源: 网络整理| 查看: 265

A scrollable, 2D array of widgets.

The main axis direction of a grid is the direction in which it scrolls (the scrollDirection).

The most commonly used grid layouts are GridView.count, which creates a layout with a fixed number of tiles in the cross axis, and GridView.extent, which creates a layout with tiles that have a maximum cross-axis extent. A custom SliverGridDelegate can produce an arbitrary 2D arrangement of children, including arrangements that are unaligned or overlapping.

To create a grid with a large (or infinite) number of children, use the GridView.builder constructor with either a SliverGridDelegateWithFixedCrossAxisCount or a SliverGridDelegateWithMaxCrossAxisExtent for the gridDelegate.

To use a custom SliverChildDelegate, use GridView.custom.

To create a linear array of children, use a ListView.

To control the initial scroll offset of the scroll view, provide a controller with its ScrollController.initialScrollOffset property set.

Transitioning to CustomScrollView

A GridView is basically a CustomScrollView with a single SliverGrid in its CustomScrollView.slivers property.

If GridView is no longer sufficient, for example because the scroll view is to have both a grid and a list, or because the grid is to be combined with a SliverAppBar, etc, it is straight-forward to port code from using GridView to using CustomScrollView directly.

The key, scrollDirection, reverse, controller, primary, physics, and shrinkWrap properties on GridView map directly to the identically named properties on CustomScrollView.

The CustomScrollView.slivers property should be a list containing just a SliverGrid.

The childrenDelegate property on GridView corresponds to the SliverGrid.delegate property, and the gridDelegate property on the GridView corresponds to the SliverGrid.gridDelegate property.

The GridView, GridView.count, and GridView.extent constructors' children arguments correspond to the childrenDelegate being a SliverChildListDelegate with that same argument. The GridView.builder constructor's itemBuilder and childCount arguments correspond to the childrenDelegate being a SliverChildBuilderDelegate with the matching arguments.

The GridView.count and GridView.extent constructors create custom grid delegates, and have equivalently named constructors on SliverGrid to ease the transition: SliverGrid.count and SliverGrid.extent respectively.

The padding property corresponds to having a SliverPadding in the CustomScrollView.slivers property instead of the grid itself, and having the SliverGrid instead be a child of the SliverPadding.

Once code has been ported to use CustomScrollView, other slivers, such as SliverList or SliverAppBar, can be put in the CustomScrollView.slivers list.

This example demonstrates how to create a GridView with two columns. The children are spaced apart using the crossAxisSpacing and mainAxisSpacing properties.

The GridView displays six children with different background colors arranged in two columns

link GridView.count( primary: false, padding: const EdgeInsets.all(20), crossAxisSpacing: 10, mainAxisSpacing: 10, crossAxisCount: 2, children: [ Container( padding: const EdgeInsets.all(8), color: Colors.teal[100], child: const Text("He'd have you all unravel at the"), ), Container( padding: const EdgeInsets.all(8), color: Colors.teal[200], child: const Text('Heed not the rabble'), ), Container( padding: const EdgeInsets.all(8), color: Colors.teal[300], child: const Text('Sound of screams but the'), ), Container( padding: const EdgeInsets.all(8), color: Colors.teal[400], child: const Text('Who scream'), ), Container( padding: const EdgeInsets.all(8), color: Colors.teal[500], child: const Text('Revolution is coming...'), ), Container( padding: const EdgeInsets.all(8), color: Colors.teal[600], child: const Text('Revolution, they...'), ), ], )

This example shows how to create the same grid as the previous example using a CustomScrollView and a SliverGrid.

The CustomScrollView contains a SliverGrid that displays six children with different background colors arranged in two columns

link CustomScrollView( primary: false, slivers: [ SliverPadding( padding: const EdgeInsets.all(20), sliver: SliverGrid.count( crossAxisSpacing: 10, mainAxisSpacing: 10, crossAxisCount: 2, children: [ Container( padding: const EdgeInsets.all(8), color: Colors.green[100], child: const Text("He'd have you all unravel at the"), ), Container( padding: const EdgeInsets.all(8), color: Colors.green[200], child: const Text('Heed not the rabble'), ), Container( padding: const EdgeInsets.all(8), color: Colors.green[300], child: const Text('Sound of screams but the'), ), Container( padding: const EdgeInsets.all(8), color: Colors.green[400], child: const Text('Who scream'), ), Container( padding: const EdgeInsets.all(8), color: Colors.green[500], child: const Text('Revolution is coming...'), ), Container( padding: const EdgeInsets.all(8), color: Colors.green[600], child: const Text('Revolution, they...'), ), ], ), ), ], )

By default, GridView will automatically pad the limits of the grid's scrollable to avoid partial obstructions indicated by MediaQuery's padding. To avoid this behavior, override with a zero padding property.

The following example demonstrates how to override the default top padding using MediaQuery.removePadding. link Widget myWidget(BuildContext context) { return MediaQuery.removePadding( context: context, removeTop: true, child: GridView.builder( gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount( crossAxisCount: 3, ), itemCount: 300, itemBuilder: (BuildContext context, int index) { return Card( color: Colors.amber, child: Center(child: Text('$index')), ); } ), ); }

This example shows a custom implementation of ListTile selection in a GridView or ListView. Long press any ListTile to enable selection mode. link

To create a local project with this code sample, run: flutter create --sample=widgets.GridView.4 mysample

See also:

SingleChildScrollView, which is a scrollable widget that has a single child. ListView, which is scrollable, linear list of widgets. PageView, which is a scrolling list of child widgets that are each the size of the viewport. CustomScrollView, which is a scrollable widget that creates custom scroll effects using slivers. SliverGridDelegateWithFixedCrossAxisCount, which creates a layout with a fixed number of tiles in the cross axis. SliverGridDelegateWithMaxCrossAxisExtent, which creates a layout with tiles that have a maximum cross-axis extent. ScrollNotification and NotificationListener, which can be used to watch the scroll position without using a ScrollController. The catalog of layout widgets.


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3